package com.tongguan.paypos.utils;

import android.os.Bundle;
import android.text.TextUtils;

import com.tongguan.paypos.entity.TransUnionInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by lixia on 2016/10/27.
 */

public class Utils {
    /**
     * 跳转到新大陆时的返回参数 转换
     */
    public static TransUnionInfo assembleTransUnionInfo(Bundle bundle, String transName, boolean is_trans) {
        TransUnionInfo info = null;
        if (bundle != null) {
            info = new TransUnionInfo();
            info.setTransName(transName);
            String message;
            if (is_trans) {
                message = "支付成功_";
            } else {
                message = "支付失败_";
            }
            info.setMessage(message);
            info.setAmount(bundle.getString("amount"));
            info.setTraceNo(bundle.getString("traceNo"));
            info.setReferenceNo(bundle.getString("referenceNo"));
            info.setCardNo(bundle.getString("cardNo"));
            info.setType(bundle.getString("type"));
            info.setIssue(bundle.getString("issue"));
            info.setBatchNo(bundle.getString("batchNo"));
            info.setTerminalId(bundle.getString("terminalId"));
            info.setMerchantId(bundle.getString("merchantId"));
            info.setMerchantName(bundle.getString("merchantName"));
        }
        return info;
    }

    public static String assembleTransUnionInfo(String reason, boolean is_trans) {
        if(TextUtils.isEmpty(reason)){
            return "";
        }
        JSONObject jsonObject;
        String amount = null;
        String authCode = null;
        String balance = null;
        String date = null;
        String expDate = null;
        String name = null;
        String oldTrace = null;
        String pan = null;
        String referenceNo = null;
        String time = null;
        String trace = null;
        String dsn = null;
        try {
            jsonObject = new JSONObject(reason);
            amount = jsonObject.getString("amount");//交易金额
            authCode = jsonObject.getString("authCode");//授权码
            balance = jsonObject.getString("balance");//余额
            date = jsonObject.getString("date");//交易日期
            expDate = jsonObject.getString("expDate");//卡片有效期
            name = jsonObject.getString("name");//交易名称
            oldTrace = jsonObject.getString("oldTrace");//原凭证号
            pan = jsonObject.getString("pan");//卡号
            referenceNo = jsonObject.getString("referenceNo");//参考号
            time = jsonObject.getString("time");//交易时间
            trace = jsonObject.getString("trace");//凭证号
            dsn = jsonObject.getString("dsn");//POS机序列号
        } catch (JSONException e) {
            e.printStackTrace();
        }
        StringBuffer stringBuffer = new StringBuffer();
        if (is_trans){
            stringBuffer.append("支付成功\n");
        }else {
            stringBuffer.append("支付失败\n");
        }
        stringBuffer.append("交易金额:" + amount + "\n");
        stringBuffer.append("授权码:" + authCode + "\n");
        stringBuffer.append("余额:" + balance + "\n");
        stringBuffer.append("交易日期:" + date + "\n");
        stringBuffer.append("卡片有效期:" + expDate + "\n");
        stringBuffer.append("交易名称:" + name + "\n");
        stringBuffer.append("原凭证号:" + oldTrace + "\n");
        stringBuffer.append("卡号:" + pan + "\n");
        stringBuffer.append("参考号;" + referenceNo + "\n");
        stringBuffer.append("交易时间:" + time + "\n");
        stringBuffer.append("凭证号:" + trace + "\n");
        stringBuffer.append("POS机序列号:" + dsn + "\n");
        return stringBuffer.toString();
    }

    /**
     * 拆分集合
     *
     * @param <T>
     * @param resList 要拆分的集合
     * @param count   每个集合的元素个数
     * @return 返回拆分后的各个集合
     */
    public static <T> List<List<T>> split(List<T> resList, int count) {

        if (resList == null || count < 1)
            return null;
        List<List<T>> ret = new ArrayList<List<T>>();
        int size = resList.size();
        if (size <= count) { // 数据量不足count指定的大小
            ret.add(resList);
        } else {
            int pre = size / count;
            int last = size % count;
            // 前面pre个集合，每个大小都是count个元素
            for (int i = 0; i < pre; i++) {
                List<T> itemList = new ArrayList<T>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                ret.add(itemList);
            }
            // last的进行处理
            if (last > 0) {
                List<T> itemList = new ArrayList<T>();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * count + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }

    public static String getRand32() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}
